package common

import (
	"fmt"
	"os/exec"
	"runtime"
	"strings"
)

// IsProcessExist 根据进程名判断进程是否正在运行, 不需要带exe后缀
func IsProcessExist(appName string) bool {
	var params = make([]string, 2)
	var cmd *exec.Cmd

	if runtime.GOOS == "linux" {
		params[0] = "-C"
		params[1] = appName
		cmd = exec.Command("ps", params...)
	} else if runtime.GOOS == "windows" {
		appName = appName + ".exe"
		cmd = exec.Command("tasklist")
	}

	output, err := cmd.Output()
	if err != nil {
		fmt.Println(err)
		return false
	}

	var psInfo = string(output)
	// fmt.Println(psInfo)
	if strings.Contains(psInfo, appName) {
		return true
	}

	return false
}
